<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* SELECTMETADATA class
*
* Select database metadata on a variety of parameters
*****/
class SELECTMETADATA
{
// Constructor
	function SELECTMETADATA($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/list/LISTCOMMON.php");
		$this->common = new LISTCOMMON($this->db, $this->vars);
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/html/TABLE.php");
	}
// display form options
	function display($error = FALSE)
	{
//		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar('heading', $this->messages->text("heading", "selectMeta"));
///First check, do we have metadata?
		$metadataExists = FALSE;
		$recordset = $this->db->select(array('WKX_resource_quote'), array('id'));
		if($this->db->fetchOne($recordset))
			$metadataExists = TRUE;
		if(!$metadataExists)
		{
			$recordset = $this->db->select(array('WKX_resource_paraphrase'), array('id'));
			if($this->db->fetchOne($recordset))
				$metadataExists = TRUE;
		}
		if(!$metadataExists)
		{
			$recordset = $this->db->select(array('WKX_resource_musing'), array('id'));
			if($this->db->fetchOne($recordset))
				$metadataExists = TRUE;
		}
		if(!$metadataExists)
		{
			$this->template->setVar('body', $this->messages->text('resources', 'noMetadata'));
			return $this->template->process();
		}
		$this->userBib = $this->session->getVar("mywikindx_bibliography_use");
		$bib = FALSE;
		if($this->userBib)
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->userBib));
			$row = $this->db->fetchRow($recordset);
			$bib = MISC::p(MISC::span("(" . $this->messages->text("user", "bibliography") . ": " . 
				$row['title'] . ")", "hint"));
		}
		else if($this->session->getVar('setup_multiUser'))
			$bib = MISC::p(MISC::span("(" . $this->messages->text("user", "bibliography") . ": " . 
			$this->messages->text("user", "masterBib") . ")", "hint"));
		include_once("core/html/MISC.php");
		include_once("core/html/TABLE.php");
		$pString = $error ? $error : FALSE;
		$pString .= FORM::formHeader("selectMetaProcess");
		$pString .= $bib;
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
// Types of metadata to search on
		$this->radioButtons = FALSE;
		$fields = array("quote"	=>	$this->messages->text("metadata", "quotes"),
				"quoteComment"	=>	$this->messages->text("metadata", "quoteComments"),
				"paraphrase"	=>	$this->messages->text("metadata", "paraphrases"),
				"paraphraseComment"	=>	$this->messages->text("metadata", "paraphraseComments"),
				"musing"	=>	$this->messages->text("metadata", "musings"),
			);
		$pString .= TABLE::td($this->makeFormMultiple("metadata", $fields));
// Resource types
		$this->grabTypes();
		$pString .= TABLE::td($this->makeFormMultiple("type", $this->type));
// There may not yet be tags in the database.
		$this->grabTags();
		if($this->tag)
			$pString .= TABLE::td($this->makeFormMultiple("tag", $this->tag));
		else
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= TABLE::tdEnd() . TABLE::trEnd() . TABLE::trStart() . TABLE::tdStart() . MISC::hr();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$this->grabGroups();
		$pString .= TABLE::td($this->makeFormMultiple("category", $this->group));
// There may not yet be keywords in the database.
		$this->grabKeywords();
		if($this->keyword)
			$pString .= TABLE::td($this->makeFormMultiple("keyword", $this->keyword));
		else
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::td($this->userBibs());
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= TABLE::tdEnd() . TABLE::trEnd() . TABLE::trStart() . TABLE::tdStart() . MISC::hr();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$this->grabCreators();
		if($this->creator)
			$pString .= TABLE::td($this->makeFormMultiple("creator", $this->creator));
		else
			$pString .= TABLE::td("&nbsp;");
		$this->grabPublishers();
		if($this->publisher)
			$pString .= TABLE::td($this->makeFormMultiple("publisher", $this->publisher));
		else
			$pString .= TABLE::td("&nbsp;");
		$this->grabCollections();
		if($this->collection)
			$pString .= TABLE::td($this->makeFormMultiple("collection", $this->collection, 60));
		else
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		if(is_array($this->session->getArray("selectMeta")))
		{
			$this->session->clearArray("selectMeta");
			$reset = FORM::formHeader("selectMetaDisplay") . 
				FORM::formSubmit("Reset") . FORM::formEnd();
			$pString .= MISC::p(FORM::formSubmit("Select") . FORM::formEnd() . $reset);
		}
		else
			$pString .= MISC::br() . FORM::formSubmit("Select") . MISC::br() . MISC::br() . 
			FORM::formReset() . FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Return select box of user bibliographies for this user if browsing the master bibliography
	function userBibs()
	{
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		if(!$this->session->getVar("mywikindx_bibliographies"))
			return "&nbsp;";
		$bibs = unserialize($this->session->getVar("mywikindx_bibliographies"));
		if($this->userBib)
			unset($bibs[$this->userBib]);
		if(empty($bibs))
			return "&nbsp;";
// add 0 => IGNORE to $bibs
		$temp[0] = $this->messages->text("misc", "ignore");
		foreach($bibs as $key => $value)
			$temp[$key] = $value;
//		$pString .= FORM::hidden("selectMeta_method", "bibId");
		$selected = $this->session->getVar("selectMeta_bibId");
		if($selected && array_key_exists($selected, $bibs))
			return FORM::selectedBoxValue($this->messages->text("select", "notInUserBib"), 
			"selectMeta_bibId", $temp, $selected, 5);
		else
			return FORM::selectFBoxValue($this->messages->text("select", "notInUserBib"),
			"selectMeta_bibId", $temp, 5);
	}
// make a self-contained form with a multiple select box
	function makeFormMultiple($type, $array, $width = FALSE)
	{
// add 0 => IGNORE to $array
		if($type != 'metadata')
		{
			$temp[0] = $this->messages->text("misc", "ignore");
			foreach($array as $key => $value)
				$temp[$key] = $value;
		}
		else
			$temp = $array;
		if($selected = $this->session->getVar("selectMeta_" . $type))
		{
			$selectedArray = explode(",", $selected);
			$pString = FORM::selectedBoxValueMultiple($this->messages->text("select", $type), 
				"selectMeta_" . $type, $temp, $selectedArray, "5", $width);
		}
		else
		{
			if($type == 'metadata')	// select all initially
				$pString = FORM::selectedBoxValueMultiple($this->messages->text("select", 'metadata'), 
					"selectMeta_metadata", $temp, array_keys($array), "5");
			else
				$pString = FORM::selectFBoxValueMultiple($this->messages->text("select", $type), 
					"selectMeta_" . $type, $temp, "5", $width);
		}
		$pString .= MISC::br() . $this->messages->text("hint", "multiples") . MISC::br();
		if($this->radioButtons)
			$pString .= $this->radioButtons . MISC::br();
		return $pString;
	}
// get types from database.
	function grabTypes()
	{
		include_once("core/resource/RESOURCEMAP.php");
		$resourceMap = new RESOURCEMAP();
		include_once("core/messages/UTF8.php");
		$this->utf8 = new UTF8();
		foreach($resourceMap->types as $key)
			$this->type[$key] = $this->utf8->encodeUtf8($this->messages->text("resourceType", $key));
		$this->radioButtons = FALSE;
	}
// get tags from database.
	function grabTags()
	{
		if(!$this->session->getVar("setup_superadmin"))
		{
			$this->tag = FALSE;
			return;
		}
		include_once("core/file/TAG.php");
		$tag = new TAG($this->db);
		$this->tag = $tag->grabAll();
		$this->radioButtons = FALSE;
	}
// get groups from database.
	function grabGroups()
	{
		include_once("core/html/FORM.php");
		include_once("core/group/GROUP.php");
		$group = new GROUP($this->db);
		$this->group = $group->grabMeta();
		$this->makeRadioButtons('category');
	}
// get keywords from database.
	function grabKeywords()
	{
		include_once("core/keyword/KEYWORD.php");
		$keyword = new KEYWORD($this->db);
		$this->keyword = $keyword->grabMeta();
		$this->makeRadioButtons('keyword');
	}
// get creators from database.
	function grabCreators()
	{
		include_once("core/html/FORM.php");
		include_once("core/creator/CREATOR.php");
		$creator = new CREATOR($this->db);
		$this->creator = $creator->grabMeta();
		$this->makeRadioButtons('creator');
	}
// get publishers from database.
	function grabPublishers()
	{
		include_once("core/html/FORM.php");
		include_once("core/publisher/PUBLISHER.php");
		$publisher = new PUBLISHER($this->db);
		$this->publisher = $publisher->grabMeta();
		$this->radioButtons = FALSE;
	}
// get collections from database.
	function grabCollections()
	{
		include_once("core/html/FORM.php");
		include_once("core/collection/COLLECTION.php");
		$collection = new COLLECTION($this->db);
		$this->collection = $collection->grabMeta();
		$this->radioButtons = FALSE;
	}
// Create radio buttons for AND and OR
	function makeRadioButtons($type)
	{
		include_once("core/html/FORM.php");
		$type = 'selectMeta_' . $type . 'Method';
		if($this->session->getVar($type) == 'AND')
		{
			$pString = MISC::span(FORM::radioButton(FALSE, $type, 'OR') . " OR", "small") . MISC::br();
			$pString .= MISC::span(FORM::radioButton(FALSE, $type, 'AND', TRUE) . " AND", "small");
		}
// Default
		else
		{
			$pString = MISC::span(FORM::radioButton(FALSE, $type, 'OR', TRUE) . " OR", "small") . MISC::br();
			$pString .= MISC::span(FORM::radioButton(FALSE, $type, 'AND') . " AND", "small");
		}
		$this->radioButtons = $pString;
	}
// Process the selection
	function process($output)
	{
		$input = $this->checkInput();
		include_once("core/list/LISTMETADATACOMMON.php");
		$this->metadataCommon = new LISTMETADATACOMMON($this->db, $this->vars, $output);
		$this->allIds = $condition = array();
		$this->userId = $this->session->getVar('setup_userId');
		$this->kwq = $this->kwp = $this->kwm = FALSE;
		$conditionQ = $conditionQC = $conditionP = $conditionPC = $conditionM = FALSE;
		$this->statementAll();
		foreach($input as $type => $value)
		{
			if($type == 'type')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/type/TYPE.php");
				$method = new TYPE($this->db);
				$condition[] = "(" . $method->selectCondition(array('type' => $value, 
					'typeMethod' => $this->inputMethod['type'])) . ")";
			}
			else if($type == 'category')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/group/GROUP.php");
				$method = new GROUP($this->db);
				$condition[] = "(" . $method->selectCondition(array('category' => $value, 
					'categoryMethod' => $this->inputMethod['category'])) . ")";
			}
			else if($type == 'keyword')
			{
				include_once("core/keyword/KEYWORD.php");
				$kw = new KEYWORD($this->db);
				$this->kwq = $kw->selectConditionMetadata($input, 'quote');
				$this->kwp = $kw->selectConditionMetadata($input, 'paraphrase');
				$this->kwm = $kw->selectConditionMetadata($input, 'musing');
			}
			else if($type == 'tag')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/file/TAG.php");
				$method = new TAG($this->db);
				$condition[] = "(" . $method->selectCondition(array('tag' => $value, 
					'tagMethod' => $this->inputMethod['tag'])) . ")";
			}
			else if($type == 'creator')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/creator/CREATOR.php");
				$method = new CREATOR($this->db);
				$condition[] = "(" . $method->selectCondition(array('creator' => $value, 
					'creatorMethod' => $this->inputMethod['creator']), TRUE) . ")";
			}
			else if($type == 'publisher')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/publisher/PUBLISHER.php");
				$method = new PUBLISHER($this->db, $this->vars, $input);
				$condition[] = "(" . $method->selectCondition(array('publisher' => $value, 
					'publisherMethod' => $this->inputMethod['publisher'])) . ")";
			}
			else if($type == 'collection')
			{
				$this->inputMethod[$type] = $input[$type . 'Method'];
				include_once("core/collection/COLLECTION.php");
				$method = new COLLECTION($this->db, $this->vars, $input);
				$condition[] = "(" . $method->selectCondition(array('collection' => $value, 
					'collectionMethod' => $this->inputMethod['collection'])) . ")";
			}
			else if($type == 'bibId')
			{
				$recordSet = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
					" WHERE " . $this->db->formatField('WKX_user_bibliography.id') . "=" . 
					$this->db->tidyInput($input['bibId']));
				if($bibliography = $this->db->fetchOne($recordSet))
				{
					$ids = explode(",", $bibliography);
					$condition[] = $this->db->formatField('WKX_resource.id') ."!=" . 
						join(" AND " . $this->db->formatField('WKX_resource.id') ."!=", $ids);
				}
			}
			else if($type == 'metadata')
			{
				$selectArray = explode(",", $value);
//print_r($selectArray); print "<P>";
				if(array_search('quote', $selectArray) === FALSE)
					$this->stmtQuote = FALSE;
				if(array_search('quoteComment', $selectArray) === FALSE)
					$this->stmtQuoteComment = FALSE;
				if(array_search('paraphrase', $selectArray) === FALSE)
					$this->stmtParaphrase = FALSE;
				if(array_search('paraphraseComment', $selectArray) === FALSE)
					$this->stmtParaphraseComment = FALSE;
				if(array_search('musing', $selectArray) === FALSE)
					$this->stmtMusing = FALSE;
			}
		}
		if(!empty($condition))
			$condition = join(" AND ", $condition);
		else
			$condition = FALSE;
		$metadataCountTotal = 0;
		if($userBibCondition = $this->common->userBibCondition())
			$userBibCondition = $condition ? " AND " . $userBibCondition : " WHERE " . $userBibCondition;
		$condition = $condition ? " WHERE $condition" . $userBibCondition : $userBibCondition;
		$order = " ORDER BY " . 
			$this->db->tidyInputClause('surname') . "," . 
			$this->db->tidyInputClause('year1') . "," . 
			$this->db->tidyInputClause('title') . "," . 
			$this->db->tidyInputClause('page_start') . '+ 0'; // read varchar as integer
		if($this->stmtQuote)
		{
			if($this->kwq)
				$conditionQ = $condition ? "$condition AND (" . $this->kwq . ")" : " WHERE " . $this->kwq;
			else
				$conditionQ = $condition;
			$conditionQ .= $order;
			$stmt = $this->stmtQuote . $conditionQ;
			$metadataCount = 
				$this->metadataCommon->executeThisStmt($stmt, "text", "quotes", 
				$this->allIds, $this->userId);
			$metadataCountTotal += $metadataCount;
		}
		if($this->stmtQuoteComment)
		{
			if($this->kwq)
				$conditionQC = $condition ? "$condition AND (" . $this->kwq . ")" : " WHERE " . $this->kwq;
			else
				$conditionQC = $condition;
			$conditionQC .= $order;
			$stmt = $this->stmtQuoteComment . $conditionQC;
			$metadataCount = 
				$this->metadataCommon->executeThisStmt($stmt, "comment", 
				"quoteComments", $this->allIds, $this->userId);
			$metadataCountTotal += $metadataCount;
		}
		$pString = $this->metadataCommon->format('quotes');
		$pString .= $this->metadataCommon->format('quoteComments');
		if($this->stmtParaphrase)
		{
			if($this->kwp)
				$conditionP = $condition ? "$condition AND (" . $this->kwp . ")" : " WHERE " . $this->kwp;
			else
				$conditionP = $condition;
			$conditionP .= $order;
			$stmt = $this->stmtParaphrase . $conditionP;
			$metadataCount = 
				$this->metadataCommon->executeThisStmt($stmt, "text", 
				"paraphrases", $this->allIds, $this->userId);
			$metadataCountTotal += $metadataCount;
		}
		if($this->stmtParaphraseComment)
		{
			if($this->kwp)
				$conditionPC = $condition ? "$condition AND (" . $this->kwp . ")" : " WHERE " . $this->kwp;
			else
				$conditionPC = $condition;
			$conditionPC .= $order;
			$stmt = $this->stmtParaphraseComment . $conditionPC;
			$metadataCount = 
				$this->metadataCommon->executeThisStmt($stmt, "comment", 
				"paraphraseComments", $this->allIds, $this->userId);
			$metadataCountTotal += $metadataCount;
		}
		$pString .= $this->metadataCommon->format('paraphrases');
		$pString .= $this->metadataCommon->format('paraphraseComments');
		if($this->stmtMusing)
		{
			if($this->kwm)
				$conditionM = $condition ? "$condition AND (" . $this->kwm . ")" : " WHERE " . $this->kwm;
			else
				$conditionM = $condition;
			$conditionM .= $order;
			$stmt = $this->stmtMusing . $conditionM;
			$metadataCount = 
				$this->metadataCommon->executeThisStmt($stmt, "text", "musings", 
				$this->allIds, $this->userId);
			$metadataCountTotal += $metadataCount;
		}
		$pString .= $this->metadataCommon->format('musings');
		if(!$pString)
		{
			$this->template->setVar('heading', $this->messages->text("heading", "selectMeta"));
			$this->template->setVar('body', $this->messages->text("metadata", "noResultMeta"));
			return $this->template->process();
		}
		if($output == 'rtf')
		{
			$this->template->setVar('heading', $this->messages->text("heading", "exportRtf"));
			if(($pString = $this->metadataCommon->createRtf($pString)) === FALSE)
				$this->badInput($this->errors->text("file", "write"));
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		$this->template->setVar('heading', $this->messages->text("heading", "selectMeta"));
		$this->session->setVar("sql_lastMultiMeta", 'select');
// Write resource ids found here to session for use with previous and next icons when viewing a single resource.
		if(!empty($this->allIds))
			$this->session->setVar('list_allIds', base64_encode(serialize($this->allIds)));
		else
			$this->session->delVar("list_allIds");
// Get name of current bibliography
		$useBib = $this->session->getVar('mywikindx_bibliography_use');
		if($useBib)
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$row = $this->db->fetchRow($recordset);
			$bib = " (" . $this->messages->text("user", "bibliography") . ": " . 
				$this->html->dbToHtmlTidy($row['title']) . ")";
		}
		else
			$bib = " (" . $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib") . ")";
		$pString = MISC::p($this->messages->text("hint", "displayMeta", 
			' ' . $metadataCountTotal . $bib)) . $pString;
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Create basic select statement for all metadata tables
	function statementAll()
	{
		$this->stmtQuote = $this->db->selectNoExecute(array('WKX_resource_quote'), 
			array(array("WKX_resource_quote.id" => 'quoteId'), 'resourceId', 'text', 'page_start', 'page_end', 
				'surname'), 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_quote_text') . " ON " . 
			$this->db->formatfield("WKX_resource_quote_text.id") . "=" . 
			$this->db->formatfield("WKX_resource_quote.id"));
		$this->stmtQuoteComment = $this->db->selectNoExecute(array('WKX_resource_quote_comment'), 
			array(array("WKX_resource_quote_comment.id" => 'commentId'), 'resourceId', 
				'quoteId', 'comment', 'quotePrivate', 'WKX_resource_quote_comment.addUserIdQuote', 
				'page_start', 'page_end'), 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_quote') . " ON " . 
			$this->db->formatfield("quoteId") . "=" . 
			$this->db->formatfield("WKX_resource_quote.id"));
		$this->stmtParaphrase = $this->db->selectNoExecute(array('WKX_resource_paraphrase'), 
			array(array("WKX_resource_paraphrase.id" => 'paraphraseId'), 'resourceId', 'text', 
				'page_start', 'page_end'), 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_paraphrase_text') . " ON " . 
			$this->db->formatfield("WKX_resource_paraphrase_text.id") . "=" . 
			$this->db->formatfield("WKX_resource_paraphrase.id"));
		$this->stmtParaphraseComment = $this->db->selectNoExecute(array('WKX_resource_paraphrase_comment'), 
			array(array("WKX_resource_paraphrase_comment.id" => 'commentId'), 'resourceId', 'paraphraseId', 
				'comment', 'paraphrasePrivate', 'WKX_resource_paraphrase_comment.addUserIdParaphrase',
				'page_start', 'page_end'), 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_paraphrase') . " ON " . 
			$this->db->formatfield("paraphraseId") . "=" . 
			$this->db->formatfield("WKX_resource_paraphrase.id"));
		$this->stmtMusing = $this->db->selectNoExecute(array('WKX_resource_musing'), 
			array(array("WKX_resource_musing_text.id" => 'commentId'), 
				'resourceId', 'text', 'addUserIdMusing', 'musingPrivate', 'page_start', 'page_end'), 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_musing_text') . " ON " . 
			$this->db->formatfield("WKX_resource_musing_text.id") . "=" . 
			$this->db->formatfield("WKX_resource_musing.id"));
		$extraJoin = " LEFT JOIN " . $this->db->formatTable('WKX_resource') . " ON " . 
			$this->db->formatfield("resourceId") . "=" . 
			$this->db->formatfield("WKX_resource.id") .  
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_creator') . " ON " . 
			$this->db->formatfield("resourceId") . "=" . 
			$this->db->formatfield("WKX_resource_creator.id") . 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_misc') . " ON " . 
			$this->db->formatfield("resourceId") . "=" . 
			$this->db->formatfield("WKX_resource_misc.id") . 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_category') . " ON " . 
			$this->db->formatfield("resourceId") . "=" . 
			$this->db->formatfield("WKX_resource_category.id") . 
			" LEFT JOIN " . $this->db->formatTable('WKX_creator') . " ON " . 
			$this->db->formatfield("WKX_resource_creator.creator1") . "=" . 
			$this->db->formatfield("WKX_creator.id") . 
			" LEFT JOIN " . $this->db->formatTable('WKX_resource_year') . " ON " . 
			$this->db->formatfield("resourceId") . "=" . 
			$this->db->formatfield("WKX_resource_year.id");
		$this->stmtQuote .= $extraJoin;
		$this->stmtQuoteComment .= $extraJoin;
		$this->stmtParaphrase .= $extraJoin;
		$this->stmtParaphraseComment .= $extraJoin;
		$this->stmtMusing .= $extraJoin;
	}
// validate user input
// Input comes either from form input or from the session.
	function checkInput()
	{
		if(array_key_exists('action', $this->vars) && ($this->vars['action'] == 'metaExportRtf'))
			return $this->session->getArray("selectMeta");
// If no metadata type selected (probably from a solo resource clicking on keyword), set to all types
		if(!array_key_exists('selectMeta_metadata', $this->vars))
			$this->vars['selectMeta_metadata'] = 
			array('quote', 'quoteComment', 'paraphrase', 'paraphraseComment', 'musing');
		$inputFound = FALSE;
		$typesArray = array("metadata", "type", "tag", "category", "keyword", 
			"creator", "publisher", "collection");
		$temp = array();
		if(array_search('lastMultiMeta', $this->vars) !== FALSE)
		{
			foreach($this->session->getArray("selectMeta") as $key => $value)
			{
				if(array_search($key, $typesArray) !== FALSE)
					$this->vars['selectMeta_' . $key] = explode(',', $value);
				else
					$this->vars['selectMeta_' . $key] = $value;
			}
		}
// Delete previous stored data
		$this->session->clearArray("selectMeta");
		foreach($typesArray as $type)
		{
			$inputKey = 'selectMeta_' . $type;
			$method = "selectMeta_" . $type . "Method";
			if(!array_key_exists($inputKey, $this->vars))
				continue;
			if((sizeof($this->vars[$inputKey]) == 1) && !$this->vars[$inputKey][0]) // IGNORE
				continue;
			$inputFound = TRUE;
			if(array_key_exists(0, $this->vars[$inputKey]) && !$this->vars[$inputKey][0])
				unset($this->vars[$inputKey][0]);
			$temp[$inputKey] = implode(",", $this->vars[$inputKey]);
// If missing the following, set to default 'OR'.
			if(!array_key_exists($method, $this->vars))
				$this->vars[$method] = 'OR';
			$this->inputMethod[$type] = $this->vars[$method];
			$this->session->setVar($method, $this->vars[$method]);
		}
		if(array_key_exists('selectMeta_bibId', $this->vars) && $this->vars['selectMeta_bibId'])
		{
			$temp['selectMeta_bibId'] = $this->vars['selectMeta_bibId'];
			$inputFound = TRUE;
		}
		if(!$inputFound)
			$this->badInput($this->errors->text("inputError", "missing"));
		$this->session->writeArray($temp);
		return $this->session->getArray("selectMeta");
	}
// Error handling
	function badInput($error)
	{
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->display($error));
	}
}
?>
